devPort = 1501;
devName = "test_pis"

numBits = 50000;
numInputBits = 50000;
numRegs = 50000;
numInputRegs = 50000;

debugFlag = 1;

function initDev()
	print("initDev " .. devName);
	
	inMsg=string.char(48,0,49,0,0x4b,0x6d,0,0);
	--inMsg="abc123";
	outMsg=code_convert("UTF-16","UTF-8",inMsg,6);
	print(outMsg);
end

function simDev(func,addr,num)
	print("begin simDev" .. devName);
	
	stnNum=9;
	
	msgid=get_reg(16);
	times=get_reg_bits(17,8,8);
	priority=get_reg_bits(17,0,8);
	
	for i=0,stnNum do
	    zone="zone " .. i .. ": ";
	    for j=15,0,-1 do
	        if get_reg_bit(18+i,j)==1 then
	            zone=zone .. j .. " ";
	        else
	            zone=zone .. "*" .. " ";
	        end
	    end
	    print(zone);
	end
	
	msgLen=get_reg_bits(59,8,8);
	clear=get_reg_bit(59,4);
	atsFlag=get_reg_bits(59,2,2);
    boardFlag=get_reg_bits(59,0,2);
    
    if msgLen<3 or get_reg(60+msgLen-1)~=msgid or 
        get_reg(60+msgLen-2)~=0x5C or get_reg(60+msgLen-3)~=0x5C then
        do return end;
    end;
	
	print("message ID: " .. msgid);
	print("message times: " .. times);
	print("message priority: " .. priority);
	print("message length: " .. msgLen);
	if clear==1 then print("清除信息") end;
	if atsFlag==2 then print("ats信息关闭") end;
	if atsFlag==3 then print("ats信息打开") end;
	if boardFlag==2 then print("pis面板关闭") end;
	if boardFlag==3 then print("pis面板打开") end;
	
	inMsg="";
	
	for i=1,msgLen-3 do
	    inMsg=inMsg .. string.char(get_reg_bits(60+i-1,0,8));
	    inMsg=inMsg .. string.char(get_reg_bits(60+i-1,8,8));
	end;
	
	inMsg=inMsg .. string.char(0);
	inMsg=inMsg .. string.char(0);
	
	outMsg=code_convert("UTF-16","UTF-8",inMsg,(msgLen-2)*2);
	print("message: " .. outMsg);
	
	set_ireg_bit(300,0,1);
	set_ireg_bit(300,1,0);
	set_ireg_bit(300,2,0);
	set_ireg_bit(300,3,1);
	
	set_ireg(301,msgid);
	
	--for i=0,stnNum do
	--    set_ireg(302+i,0);
	--end
	
	--delaySim(1);
	
	--set_ireg_bit(300,0,0);
	--set_ireg_bit(300,1,0);
	--set_ireg_bit(300,2,0);
	--set_ireg_bit(300,3,1);
	
	--set_ireg(301,msgid);
	
	for i=0,stnNum do
	    set_ireg(302+i,get_reg(18+i));
	end
	
	delaySim(1);
	
	set_ireg_bit(300,0,0);
	set_ireg_bit(300,1,0);
	set_ireg_bit(300,2,0);
	set_ireg_bit(300,3,0);
	
	set_ireg(301,0);
	
	for i=0,stnNum do
	    set_ireg(302+i,0);
	end
	
	print("begin simDev" .. devName);
end

